<?php

namespace Acme\DemoBundle\Form;


use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class FormLogin extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
        ->add('user_name', 'text', array(
                'label' => "Username",
                'max_length' => 50,
                'attr' => array(
                        'style' => 'width:200px',
                )
        ))
        ->add('password', 'password', array(
                'label' => "Password",
                'max_length' => 50,
                'attr' => array(
                        'style' => 'width:200px'
                        )
        ))
        ->add('submit', 'submit', array(
                'label' => "Login",
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        // validate
        $collectionConstraint = new Collection(array(
            'user_name' => array(
                new NotBlank(array('message' => "* User Name should not be blank.")),
                new Length(array('min' => 4, 'minMessage'=> '* Too short'))
            ),
            'password' => array(
                new NotBlank(array('message' => '* Password should not be blank.'))

            )
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint,
            'csrf_protection' => false,
        ));
    }

    public function getName()
    {
        return '';
    }
}
